package org.tigris.atlas.xml.schema;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.StringUtils;
import org.tigris.atlas.bizobj.TransferObjectAssembler;

public abstract class SchemaAssembler extends TransferObjectAssembler {
	
	/**
	 * Performs a transformation from primitive to Obejct types
	 * @param inbound The primitive type
	 * @return The <tt>Object</tt> 
	 */
	public static final Long transformInboundData(long inbound) {
		return new Long(inbound); 
	}
	
	/**
	 * Performs a transformation from primitive to Obejct types
	 * @param inbound The primitive type
	 * @return The <tt>Object</tt> 
	 */
	public static final Integer transformInboundData(int inbound) {
		return new Integer(inbound); 
	}
	
	/**
	 * Performs a transformation from primitive to Obejct types
	 * @param inbound The primitive type
	 * @return The <tt>Object</tt> 
	 */
	public static final String transformInboundData(String inbound) {
		return (StringUtils.isNotBlank(inbound)) ? inbound : null; 
	}
	
	/**
	 * Performs a transformation from primitive to Obejct types
	 * @param inbound The primitive type
	 * @return The <tt>Object</tt> 
	 */
	public static final BigDecimal transformInboundData(BigDecimal inbound) {
		return inbound; 
	}
	
	/**
	 * Performs a transformation from primitive to Obejct types
	 * @param inbound The primitive type
	 * @return The <tt>Object</tt> 
	 */
	public static final Timestamp transformInboundData(Calendar inbound) {
		return (inbound != null) ? new Timestamp(inbound.getTime().getTime()) : null; 
	}
	
	/**
	 * Performs a transformation from primitive to Obejct types
	 * @param inbound The primitive type
	 * @return The <tt>Object</tt> 
	 */
	public static final Boolean transformInboundData(boolean inbound) {
		return new Boolean(inbound); 
	}	
	
	/**
	 * Performs a transformation from Obejct to primitive
	 * @param inbound The <tt>Object</tt> type
	 * @return The primitive 
	 */
	public static final long transformOutboundData(Long outbound) {
		return (outbound != null) ? outbound.longValue() : 0;
	}
	
	/**
	 * Performs a transformation from Obejct to primitive
	 * @param inbound The <tt>Object</tt> type
	 * @return The primitive 
	 */
	public static final int transformOutboundData(Integer outbound) {
		return (outbound != null) ? outbound.intValue() : 0;
	}
	
	/**
	 * Performs a transformation from Obejct to primitive
	 * @param inbound The <tt>Object</tt> type
	 * @return The primitive 
	 */
	public static final String transformOutboundData(String outbound) {
		return outbound; 
	}
	
	/**
	 * Performs a transformation from Obejct to primitive
	 * @param inbound The <tt>Object</tt> type
	 * @return The primitive 
	 */
	public static final BigDecimal transformOutboundData(BigDecimal outbound) {
		return outbound; 
	}
	
	/**
	 * Performs a transformation from Obejct to primitive
	 * @param inbound The <tt>Object</tt> type
	 * @return The primitive 
	 */
	public static final Calendar transformOutboundData(Date outbound) {
		Calendar calendar = null;
		if (outbound != null) {
			calendar = new GregorianCalendar();
			calendar.setTime(outbound);
		}
		return calendar; 
	}
	
	/**
	 * Performs a transformation from Obejct to primitive
	 * @param inbound The <tt>Object</tt> type
	 * @return The primitive 
	 */
	public static final boolean transformOutboundData(Boolean outbound) {
		return (outbound != null) ? outbound.booleanValue() : Boolean.FALSE.booleanValue();
	}
	
	public static final Collection transformInboundData(long [] in) {
		Collection out = new ArrayList();
		if (in == null) {
			return out;
		}
		
		for (int i = 0; i < in.length; i++) {
			out.add(transformInboundData(in[i]));
		}
		
		return out;
	}
	
	public static final Collection transformInboundData(Calendar [] in) {
		Collection out = new ArrayList();
		if (in == null) {
			return out;
		}
		
		for (int i = 0; i < in.length; i++) {
			out.add(transformInboundData(in[i]));
		}
		
		return out;
	}
	
	public static final Collection transformInboundData(boolean [] in) {
		Collection out = new ArrayList();
		if (in == null) {
			return out;
		}
		
		for (int i = 0; i < in.length; i++) {
			out.add(transformInboundData(in[i]));
		}
		
		return out;
	}
	
	public static final Collection transformInboundData(int [] in) {
		Collection out = new ArrayList();
		if (in == null) {
			return out;
		}
		
		for (int i = 0; i < in.length; i++) {
			out.add(transformInboundData(in[i]));
		}
		
		return out;
	}
	
	public static final Collection transformInboundData(BigDecimal [] in) {
		Collection out = new ArrayList();
		if (in == null) {
			return out;
		}
		
		for (int i = 0; i < in.length; i++) {
			out.add(transformInboundData(in[i]));
		}
		
		return out;
	}
	
	public static final Collection transformInboundData(String [] in) {
		Collection out = new ArrayList();
		if (in == null) {
			return out;
		}
		
		for (int i = 0; i < in.length; i++) {
			out.add(transformInboundData(in[i]));
		}
		
		return out;
	}

}
